package com.qindian.coupontest;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qindian.model.GoodsTypeModel;

import java.util.ArrayList;
import java.util.List;

public class GoodsListActivity2 extends BaseActivity implements View.OnClickListener {

    private TabLayout mTabLayout;
    private ViewPager mPager;
    private String mPlatform;
    private String mType;
    private String mDetail;
    private List<Fragment> mFragmentList;
    private ImageView mImgBack;
    private EditText mEtSearch;
    private List<GoodsTypeModel> mGoodsTypeModels;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_list2);
        mTabLayout = findViewById(R.id.layout_tab);
        mPager = findViewById(R.id.pager);
        mImgBack=findViewById(R.id.img_back);
        mImgBack.setOnClickListener(this);
        mEtSearch=findViewById(R.id.et_search);
        mEtSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                hideKeyboard(v);
                if (TextUtils.isEmpty(v.getText().toString())) {
                    showToast("请输入商品查询内容");
                }else {
                    Intent intent=new Intent(GoodsListActivity2.this,SearchGoodsResultActivity.class);
                    intent.putExtra("keyword",v.getText().toString());
                    intent.putExtra("platform",mPlatform);
                    intent.putExtra("type",mType);
                    intent.putExtra("detail",mDetail);
                    startActivity(intent);
                    mEtSearch.setText("");
                }
                return true;
            }
        });
        mFragmentList = new ArrayList<>();
        mGoodsTypeModels = new ArrayList<>();
        Bundle bundle = getIntent().getExtras();
        if (bundle == null)
            return;
        mPlatform = getIntent().getStringExtra("platform");
        mType = getIntent().getStringExtra("type");
        mDetail = getIntent().getStringExtra("detail");
        switch (mType) {
            case "jd":
                mGoodsTypeModels.add(new GoodsTypeModel("1", "女装"));
                mGoodsTypeModels.add(new GoodsTypeModel("2", "男装"));
                mGoodsTypeModels.add(new GoodsTypeModel("3", "数码家电"));
                mGoodsTypeModels.add(new GoodsTypeModel("4", "母婴玩具"));
                mGoodsTypeModels.add(new GoodsTypeModel("5", "美妆个护"));
                mGoodsTypeModels.add(new GoodsTypeModel("6", "食品保健"));
                mGoodsTypeModels.add(new GoodsTypeModel("7", "内衣配饰"));
                mGoodsTypeModels.add(new GoodsTypeModel("12", "居家生活"));
                mGoodsTypeModels.add(new GoodsTypeModel("13", "鞋品箱包"));
                mGoodsTypeModels.add(new GoodsTypeModel("16", "运动户外"));
                mGoodsTypeModels.add(new GoodsTypeModel("17", "文体车品"));
                mGoodsTypeModels.add(new GoodsTypeModel("18", "其它"));
                break;
            case "tb":
                mGoodsTypeModels.add(new GoodsTypeModel("12", "女装"));
                mGoodsTypeModels.add(new GoodsTypeModel("13", "男装"));
                mGoodsTypeModels.add(new GoodsTypeModel("14", "内衣"));
                mGoodsTypeModels.add(new GoodsTypeModel("15", "母婴"));
                mGoodsTypeModels.add(new GoodsTypeModel("16", "化妆品"));
                mGoodsTypeModels.add(new GoodsTypeModel("17", "居家"));
                mGoodsTypeModels.add(new GoodsTypeModel("18", "鞋包配饰"));
                mGoodsTypeModels.add(new GoodsTypeModel("19", "美食"));
                mGoodsTypeModels.add(new GoodsTypeModel("20", "车品"));
                mGoodsTypeModels.add(new GoodsTypeModel("21", "数码家电"));
                mGoodsTypeModels.add(new GoodsTypeModel("22", "其它"));
                break;
        }
        for (int i = 0; i < mGoodsTypeModels.size(); i++) {
            boolean flag=(i==0);
            mFragmentList.add(GoodsListFragment.newInstance(flag,mPlatform, mType, mDetail, mGoodsTypeModels.get(i).getCatId(),null));
        }
        mPager.setOffscreenPageLimit(mFragmentList.size());
        mPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }
            @Override
            public int getCount() {
                return mFragmentList.size();
            }
            @Override
            public CharSequence getPageTitle(int position) {
                return mGoodsTypeModels.get(position).getName();
            }
        });
        mTabLayout.setupWithViewPager(mPager);
        mTabLayout.clearOnTabSelectedListeners();
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mPager.setCurrentItem(tab.getPosition(),false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        finish();
    }
    private void hideKeyboard( View view){
        InputMethodManager manager= (InputMethodManager) GoodsListActivity2.this
                .getApplicationContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (manager != null) {
            manager.hideSoftInputFromWindow(view.getWindowToken(),0);
        }
    }
}
